﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RATSharp.Rest;
using System.Threading.Tasks;
using RATSharp.Model;

namespace RATSharpW7.ViewModel
{
    /// <summary>
    /// ModelView for ReturnItBack activity.
    /// </summary>
    public class ReturnItBackViewModel : ReservationViewModel
    {
        public ReturnItBackViewModel()
            : base()
        {
        }

        /// <summary>
        /// load all not returned periods
        /// </summary>
        public void loadNotReturnedItems() 
        {
            Task<Reservations> task = RestService.getInstance().getAllUnreturnedItems();
            notifyChangeModel(task);
        }

        /// <summary>
        /// return period
        /// </summary>
        /// <param name="period">period to return</param>
        /// <param name="handler">Rest handler</param>
        public void returnItem(period period, EventHandler handler)
        {
            Task<Reservations> task = RestService.getInstance().returnItem(period.id);
            notifyReturnedItem(task, handler);
        }

        protected void notifyReturnedItem(Task<Reservations> task, EventHandler eventHandler)
        {
            task.ContinueWith(t =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    eventHandler(this, new EventArgs());
                });
            });
        }

    }
}
